package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.MediaThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.entity.WmNewsMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.heima.media.service.IWmNewsMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author wsj
 * @since 2022-03-11
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    @Autowired
    private MinIOService minIOService;

    @Override
    public ResponseResult<WmMaterial> upload(MultipartFile file) {
        //获取线程中的user
        User user = MediaThreadLocalUtil.get();
        if (user==null) {
            //如果用户不存在，返回提示：需要登录
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //上传图片到minio
        String url = minIOService.upload(file);
        //保存图片到素材表
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(user.getId());
        wmMaterial.setUrl(url);
        wmMaterial.setType(0);
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection(false);
        save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult listByPage(WmMaterialDto dto) {
        //获取线程中的user
        User user = MediaThreadLocalUtil.get();
        if (user==null) {
            //如果用户不存在，返回提示：需要登录
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //构建分页条件
        IPage<WmMaterial> page=new Page<>(dto.getPage(),dto.getSize());
        //构建查询条件
        LambdaQueryWrapper<WmMaterial> query=new LambdaQueryWrapper<>();
        query.eq(WmMaterial::getUserId,user.getId());
        query.orderByDesc(WmMaterial::getCreatedTime);
        if (dto.getIsCollection()!=null) {
            query.eq(WmMaterial::getIsCollection,1);
        }
        //查询
        IPage<WmMaterial> iPage = page(page, query);
        //返回分页对象
        return new PageResponseResult(dto.getPage(), dto.getSize(), iPage.getTotal(), iPage.getRecords());

    }

    @Override
    public ResponseResult collect(Integer id) {
        //构建修改条件
        LambdaUpdateWrapper<WmMaterial> query=new LambdaUpdateWrapper<>();
        query.set(WmMaterial::getIsCollection, true);
        query.eq(WmMaterial::getId, id);
        //修改
        update(query);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult collectCancel(Integer id) {
        //构建修改条件
        LambdaUpdateWrapper<WmMaterial> query=new LambdaUpdateWrapper<>();
        query.set(WmMaterial::getIsCollection, false);
        query.eq(WmMaterial::getId, id);
        //修改
        update(query);
        return ResponseResult.okResult();
    }

    @Autowired
    private IWmNewsMaterialService newsMaterialService;
    @Override
    public ResponseResult delete(Integer id) {
        //判断素材是否被引用，引用则无法删除
        LambdaQueryWrapper<WmNewsMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNewsMaterial::getMaterialId, id);
        int count = newsMaterialService.count(wrapper);
        if (count>0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.MATERIAL_IS_QUOTE);
        }
        //没有引用，则删除
        removeById(id);
        return ResponseResult.okResult();
    }
}
